package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.Grade;
import com.think.teaching.service.impl.GradeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 年级 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "年级管理接口")
public class GradeController {
	
	private final GradeServiceImpl gradeService;

	public GradeController(GradeServiceImpl gradeService) {
		this.gradeService = gradeService;
	}

	@ApiOperation("添加新的年级信息")
	@PostMapping("admin/grade")
	public Result AddNewGrade(@RequestBody()Grade data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			Grade grade = gradeService.AddNewGrade(data);
			if (grade != null) {
				return Result.success(grade);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("更新的年级信息")
	@RequestMapping(value = "admin/grade",method = RequestMethod.PUT)
	public Result UpdateGrade(@RequestBody()Grade data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			Grade grade = gradeService.UpdateGrade(data);
			if (grade != null) {
				return Result.success(grade);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("删除年级信息")
	@RequestMapping(value = "admin/grade/{id}",method = RequestMethod.DELETE)
	public Result DeleteGrade(@PathVariable("id")Integer id) {
		Grade grade = gradeService.getById(id);
		if (grade == null) {
			return Result.failure(ResultCode.GRADE_NOT_EXIST);
		} else {
			if (gradeService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取年级信息详情")
	@RequestMapping(value = "admin/grade/{id}",method = RequestMethod.GET)
	public Result GetGrade(@PathVariable("id")Integer id) {
		Grade grade = gradeService.getById(id);
		if (grade == null) {
			return Result.failure(ResultCode.GRADE_NOT_EXIST);
		} else {

			return Result.success(grade);
		}
	}
	
	@ApiOperation("管理员修改年级状态")
	@RequestMapping(value = "admin/grade/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateGradeState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		Grade grade = gradeService.getById(id);
		if (grade == null) {
			return Result.failure(ResultCode.GRADE_NOT_EXIST);
		} else {
			try {
				grade = gradeService.UpdateGradeState(id, state);
				return Result.success(grade);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	@ApiOperation("用户搜索年级")
	@RequestMapping(value = "user/grade/search",method = RequestMethod.GET)
	public Result UserSearchGrade(@RequestParam("key")String key) {
		return Result.success(gradeService.SearchGradeWithKey(key, true));
	}
	
	@ApiOperation("管理员搜索年级")
	@RequestMapping(value = "admin/grade/search",method = RequestMethod.GET)
	public Result AdminSearchGrade(@RequestParam("key")String key) {
		return Result.success(gradeService.SearchGradeWithKey(key, false));
	}
	
	@ApiOperation("管理员搜索年级列表")
	@RequestMapping(value = "admin/grades/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListGrade(@PathVariable("page")Integer page,@PathVariable("count")Integer count,
			@RequestParam("tag") Integer tag, Integer type) {
		return Result.success(gradeService.ListGradeWithTagAndType(count, page, type, tag, false));
	}
	
	@ApiOperation("用户搜索年级列表")
	@RequestMapping(value = "user/grades/{page}/{count}",method = RequestMethod.GET)
	public Result UserListGrade(@PathVariable("page")Integer page,@PathVariable("count")Integer count,
			@RequestParam("tag") Integer tag, Integer type) {
		return Result.success(gradeService.ListGradeWithTagAndType(count, page, type, tag, true));
	}
}

